﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Quebec.Silverlight.Animations
{
	public class OpacityAnimation : AnimationBase
	{
		public static TimeSpan DefaultDuration = TimeSpan.FromMilliseconds(250);
		public static TimeSpan DefaultBeginTime = TimeSpan.FromMilliseconds(0);

		public OpacityAnimation(double opacity)
			: this(opacity, DefaultDuration, DefaultBeginTime)
		{
		}

		public OpacityAnimation(double opacity, TimeSpan duration)
			: this(opacity, duration, DefaultBeginTime)
		{
		}

		public OpacityAnimation(double opacity, TimeSpan duration, TimeSpan beginTime)
		{
			Opactity = opacity;
			Duration = duration;
			BeginTime = beginTime;
		}

		public double Opactity
		{
			get;
			private set;
		}

		public TimeSpan Duration
		{
			get;
			private set;
		}

		public TimeSpan BeginTime
		{
			get;
			private set;
		}

		protected override Storyboard CreateStoryboard()
		{
			var storyboard = new Storyboard();
			var opacityAnimation = new DoubleAnimation();

			Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath("(FrameworkElement.Opacity)"));
			storyboard.Children.Add(opacityAnimation);

			return storyboard;
		}

		protected override void ApplyValues(Storyboard storyboard)
		{
			if (storyboard == null)
				throw new ArgumentNullException("storyboard");

			var opacityAnimation = storyboard.Children[0] as DoubleAnimation;

			opacityAnimation.To = Opactity;
			opacityAnimation.BeginTime = BeginTime;
			opacityAnimation.Duration = Duration;
		}
	}
}
